// pages/myapiary/add/add.js
import http from '../../../http/http.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    province:[],
    city:[],
    honeyTypelist:[],
    areachoice:false,
    provinceStr:'',
    cityStr:'',
    pcastr:null,
    ctindex:0,
    activeindex:-1,
    pars:{
      name:'',
      province:'',
      city:'',
      address:'',
      ownerContact:'',
      beeType:'',
      honeyType:'',
      descriptions:'',
      lat:'',
      lng:''
    },
    active_name:false,
    active_address:false,
    active_phone:false,
    active_honey:false,
    active_descript:false,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this=this
    this.initpc()
    this.beetype()
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        _this.setData({
          ['pars.lat']: res.latitude,
          ['pars.lng']: res.longitude,
        })
      },
    })
  },
  // 蜜蜂类型
  beetype:function(){
    http.getdata('/api/apiary/species/list').then((res)=>{
      this.setData({
        honeyTypelist:res.data.result
      })
    })
  },
  // 初始化省市
  initpc:function(){
    http.getdata('/api/region/list?parentId=0').then((res) => {
      this.setData({
        province: res.data.result,
        ['pars.province']: res.data.result[0].id
      })
      http.getdata('/api/region/list?parentId=' + res.data.result[0].id).then((ctityres) => {
        this.setData({
          city: ctityres.data.result,
          ['pars.city']: ctityres.data.result[0].id
        })
      })
    })
  },
  // 省
  bindChange:function(e){
    const val = e.detail.value
    let index = e.detail.value[0]
    let cityindex = e.detail.value[1]
    let proviceid=this.data.province[index].id
    console.log(e)
    http.getdata('/api/region/list?parentId=' + proviceid).then((res) => {
      this.setData({
        city: res.data.result,
        ['pars.province']: proviceid,
        provinceStr: this.data.province[index].name,
        cityStr: this.data.city[cityindex].name,
        ctindex: e.detail.value[1]

      })
    })
  },
  // 定位
  position:function(){
    var _this=this
    wx.chooseLocation({
      success: function(res) {
        _this.setData({
          ['pars.address']:res.address,
          ['pars.lat']: res.latitude,
          ['pars.lng']: res.longitude,
        })
      },
    })
  },
  // 获取
  getvalue:function(e){
    let type = e.currentTarget.dataset.type
    let val = e.detail.value
    if(type=='name'){
      this.data.pars.name=val
    }else if(type=='address'){
      this.data.pars.address = val
    }else if(type=='phone'){
      this.data.pars.ownerContact = val
    }else if(type=='honey'){
      this.data.pars.honeyType = val
    }else{
      this.data.pars.descriptions = val
    }
  },
  active: function (e) {
    let type = e.currentTarget.dataset.type
    let val = e.detail.value
    if (type == 'name') {
      this.setData({
        active_name: true,
        active_address: false,
        active_phone: false,
        active_honey: false,
        active_descript: false,
      })
    } else if (type == 'address') {
      this.setData({
        active_name: false,
        active_address: true,
        active_phone: false,
        active_honey: false,
        active_descript: false,
      })
    } else if (type == 'phone') {
      this.setData({
        active_name: false,
        active_address: false,
        active_phone: true,
        active_honey: false,
        active_descript: false,
      })
    } else if (type == 'honey') {
      this.setData({
        active_name: false,
        active_address: false,
        active_phone: false,
        active_honey: true,
        active_descript: false,
      })
    } else {
      this.setData({
        active_name: false,
        active_address: false,
        active_phone: false,
        active_honey: false,
        active_descript: true,
      })
    }
  },
  // 类型选择
  bindPickerChange:function(e){
    let val = e.detail.value
    this.setData({
      ['pars.beeType']: this.data.honeyTypelist[val].code,
      activeindex:val
    })
  },

  sure_area:function(){
    var value = this.data.provinceStr +" "+this.data.city[this.data.ctindex].name
    this.setData({
      pcastr: value,
      areachoice:false
    })
    // this.initpc()
  },
  // 取消
  canel:function(){
    this.setData({
      areachoice:false
    })
  },
  // 确认
  sure:function(){
    this.initpc()
    this.setData({
      areachoice: true
    })
  },
  // 提交
  submit:function(){
    this.data.pars.city = this.data.city[this.data.ctindex].id
    console.log(this.data.pars)
    if(this.data.pars.name==''){
        wx.showToast({
          title: '请输入蜂场名称',
          icon:'none',
          mask:true
        })
        return false
    } else if (this.data.pcastr == '' || this.data.pcastr ==null ){
      wx.showToast({
        title: '请选择蜂场区域',
        icon: 'none',
        mask: true
      })
      return false
    } else if (this.data.pars.address == '') {
      wx.showToast({
        title: '详细地址不能为空',
        icon: 'none',
        mask: true
      })
      return false
    } else if (this.data.pars.ownerContact == '') {
      wx.showToast({
        title: '请输入管理员电话',
        icon: 'none',
        mask: true
      })
      return false
    } else if (this.data.pars.beeType == '') {
      wx.showToast({
        title: '请选择蜜蜂品种',
        icon: 'none',
        mask: true
      })
      return false
    } else if (this.data.pars.honeyType == '') {
      wx.showToast({
        title: '请输入蜜源品种',
        icon: 'none',
        mask: true
      })
      return false
    } else if (this.data.pars.descriptions == '') {
      wx.showToast({
        title: '请输入蜂场简介',
        icon: 'none',
        mask: true
      })
      return false
    }else{
      http.postdata('/api/apiary/add', this.data.pars).then((res) => {
        wx.redirectTo({
          url: '../list/myapiary',
        })
      })
    }
  }
})